package com.fare.taluo.web.action.detect.sys;

import org.apache.struts2.convention.annotation.Namespace;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.util.value.NumberUtils;
import com.fare.taluo.web.action.detect.ComputeInfoAction;
/**
 * 内存监控
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-11 下午1:51:22
 * @version v1.0
 */
@Namespace("/")
@Controller("mem")
@Scope("prototype")
public class MemAction extends ComputeInfoAction
{

	private static final long serialVersionUID = 0x1d55e9090e7c2fb9L;
	private Sigar sigar = null;
	private double total;
	private double used;
	private double free;
	private double actualUsed;
	private double actualFree;
	private double percUsed;
	private double percFree;
	private double percActualUsed;
	private double percActualFree;

	public MemAction(){}

	public String show() throws Exception{
		Mem mem = null;
		try
		{
			sigar = new Sigar();
			mem = sigar.getMem();
			total = NumberUtils.div(mem.getTotal(), 1048576D, 2);
			free = NumberUtils.div(mem.getFree(), 1048576D, 2);
			used = NumberUtils.div(mem.getUsed(), 1048576D, 2);
			actualFree = NumberUtils.div(mem.getActualFree(), 1048576D, 2);
			actualUsed = NumberUtils.div(mem.getActualUsed(), 1048576D, 2);
		}
		catch (SigarException e)
		{
			e.printStackTrace();
			logger.error("获取服务器内存数据出错", e);
		}finally{
			if (sigar != null)
				sigar.close();
			mem = null;
			sigar = null;
		}
		returnPageURL = "/system/detect/mem.jsp";
		return "dispatcher";
	}

	public Sigar getSigar()
	{
		return sigar;
	}

	public void setSigar(Sigar sigar)
	{
		this.sigar = sigar;
	}

	public double getTotal()
	{
		return total;
	}

	public void setTotal(double total)
	{
		this.total = total;
	}

	public double getUsed()
	{
		return used;
	}

	public void setUsed(double used)
	{
		this.used = used;
	}

	public double getFree()
	{
		return free;
	}

	public void setFree(double free)
	{
		this.free = free;
	}

	public double getActualUsed()
	{
		return actualUsed;
	}

	public void setActualUsed(double actualUsed)
	{
		this.actualUsed = actualUsed;
	}

	public double getActualFree()
	{
		return actualFree;
	}

	public void setActualFree(double actualFree)
	{
		this.actualFree = actualFree;
	}

	public double getPercUsed()
	{
		if (getTotal() != 0.0D)
			percUsed = NumberUtils.div(getUsed(), getTotal(), 4);
		return percUsed;
	}

	public void setPercUsed(double percUsed)
	{
		this.percUsed = percUsed;
	}

	public double getPercFree()
	{
		if (getTotal() != 0.0D)
			percFree = NumberUtils.div(getFree(), getTotal(), 4);
		return percFree;
	}

	public void setPercFree(double percFree)
	{
		this.percFree = percFree;
	}

	public double getPercActualUsed()
	{
		if (getTotal() != 0.0D)
			percActualUsed = NumberUtils.div(getActualUsed(), getTotal(), 4);
		return percActualUsed;
	}

	public void setPercActualUsed(double percActualUsed)
	{
		this.percActualUsed = percActualUsed;
	}

	public double getPercActualFree()
	{
		if (getTotal() != 0.0D)
			percActualFree = NumberUtils.div(getActualFree(), getTotal(), 4);
		return percActualFree;
	}

	public void setPercActualFree(double percActualFree)
	{
		this.percActualFree = percActualFree;
	}
}
